Task References

On this page:

sort (array)

Purpose

The sort task sorts the elements of a given array and returns the sorted array. The default sort order is ascending (e.g., from A to Z or from 0 to 9). This task method changes the position of the elements in the original array.

Potential Use Case

The sort task can be used to sort arrays of numbers, strings and objects. You could use this task to arrange a list of names in alphabetical order, or a collection of dates in numerical order. For any outcome data that needs to be captured and sorted, you could potentially use sort .

Properties

Input and output properties are shown below.

Input Type Description
arr Array Required. The array to be sorted.
key String Optional. For objects, a value used to set the sort order.
limit Number Optional. The number of records to return.
reverse Boolean Required. If true, the arr is sorted in reverse (descending) order. Default is false (ascending order).


Output Type Description
response Array The output of sorting the arr.

Examples

Example 1

In this IAP example:

  • An array of incoming reference variables for arr are statically set:
    [{"name": "cat"},{"name": "dog"},{"name": "hamster"},{"name": "ant"},{"name": "Lion"}].
    Notice the name "Lion" is uppercase.

  • The key parameter is name, and the record limit is 4.

  • The reverse parameter is false (default).

    sort - job variables

  • The sort order will be ascending in the response, with "Lion" listed first in the sorted array since it begins with a capital (uppercase) letter.

    sort - job variables

Example 2

In this IAP example:

  • An array of incoming variables for arr are statically set. The reference variables are:
    [{"car": "Audi"},{"car": "Volvo"},{"car": "Honda"},{"car": "BMW"}]. Notice all the initial letters in the names are uppercase.

  • The key parameter is car, and the limit is left blank.

  • The reverse parameter is false (default).

    sort - job variables

  • The resulting sort order will be alphabetical (ascending order).

    sort - job variables

Example 3

In this IAP example:

  • An array of incoming variables for arr are statically set. The reference variables are:
    ["ant","bee","Cat","dog","Elephant"]. Notice the mix of lowercase and uppercase values.

  • The key and limit are left blank.

  • The reverse parameter is true.

    sort - job variables

  • The resulting sort order will be descending, with all the lowercase values listed in reverse alphabetical order followed by the uppercase values.

    sort - job variables